import 'package:flutter/material.dart';

class Dart_Base_DataType_Page extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    void _test() {
      print("!!!!!!!!!!!!!!!!!!!!!!  Number  !!!!!!!!!!!!!!!!!!!!");
      int a = 1;
      int b = 10;
      int c = 0xDEADBEEF;

      double d = 1.0;
//      double e = 1;// 运行时报错 Try changing the type of the left hand side, or casting the right hand side to 'dart.core::double'
      double f = 10.0;
      print("a:$a  b:$b   c:$c   d:$d    f:$f  ");
      print("a + b=${a + b}     ,a + d=${a + d}");
      print("a - b=${a - b}     ,a - d=${a - d}");
      print("a * b=${a * b}     ,a * d=${a * d}");
      print("a / b=${a / b}     ,a / d=${a / d}");
      print("a ~/ b=${a ~/ b}     ,a ~/ d=${a ~/ d}");
      print("a % b=${a % b}     ,a % d=${a % d}");

      print("1是NaN吗？${a.isNaN}，1是奇数吗？${a.isOdd}，1是偶数吗？${a.isEven}，1是有限数吗？${a
          .isFinite},1是无限数吗？${a.isInfinite},1是负数吗？${a.isNegative}");

//      Number转字符串toString()
//      字符串转Number，parse()
      print("${a.toString()}");
      String age = "12";
      print("${int.parse(age)}");

      print("!!!!!!!!!!!!!!!!!!!!!!  String  !!!!!!!!!!!!!!!!!!!!");
      //可以使用 单引号 和 双引号声明字符串
      var name = "zhangsan";
      var clazz = 'one';

      //单、双引号可以互相嵌套，转义不转义效果一样
      var str1 = "abcd'efg'";
      print(str1);
      var str4 = "abcd\'efg\'";
      print(str4);

      var str2 = 'abcd"efg"';
      print(str2);
      var str3 = 'abcd\"efg\"';
      print(str3);

      print("abcdefghi \n jkl \t mn");

      print("!!!!!!!!!!!!!!!!!!!!!!  Boolean  !!!!!!!!!!!!!!!!!!!!");
      var m;
      print("var m 是null吗${m == null}");
      m = 1 / 0;
      print("1 / 0是NaN吗？${m.isNaN}");
      m = 0 / 0;
      print("0 / 0是NaN吗？${m.isNaN}");
      m = 0 / 0;
      print("0 / 1是NaN吗？${m.isNaN}");
      m = "";
      print("''是空吗？${m.isEmpty}");
      m = " ";
      print("' '是空串吗？${m.isEmpty}");


      //List
      print("!!!!!!!!!!!!!!!!!!!!!!  List  !!!!!!!!!!!!!!!!!!!!");
      var list = List<int>();
      list.add(1);
      list.add(2);
      print("list的长度：${list.length}");

      var list1 = new List<int>();
      var list2 = [1, 2];
      var list3 = <int>[];

      var list4 = const[1, 2]; // list4 指向的是一个常量，我们不能给它添加元素（不能修改它）
      // list4 本身不是一个常量，所以它可以指向另一个对象
      list4 = [4, 5, 6];

      const list5 = [1, 2]; //等价于 var list4 = const[1, 2];


      var list6 = [1, 2, 3, 4, 5, 6];
      for (var i in list6) {
        print(i);
      }


//Set
      var set = Set<String>();
      set.add("AAA");
      set.add("BBB");
      print(set.contains("AAA"));

      print("!!!!!!!!!!!!!!!!!!!!!!  Map  !!!!!!!!!!!!!!!!!!!!");
      //Map
      var map = Map<String, int>();
      map["abc"] = 1;
      map["cde"] = 2;
      map["fgh"] = 3;
      print("cde对应的值：${map["cde"]}");
      map["cde"] = 4;
      print("修改后cde对应的值：${map["cde"]}");

      if (map["mn"] == null) {
        print("key不存在");
      }
      print("map的长度:${map.length}");
      // 判断是否为空
      print(map.isEmpty); // 输出结果：false
      print(map.isNotEmpty); // 输出结果：true
      // 获取到所有的 key
      print(map.keys);
      // 获取到所有的 values
      print(map.values);
      // 判断是否包含某个key
      print(map.containsKey("cde"));
      // 判断是否包含某个value
      print(map.containsValue(5));

      var map2 = const{
        "num": 123,
        "name": "xiaoming"
      };

      var map3 = <String, String>{};
      map3["aaa"] = "lisi";
      map3["abc"] = "zhangsan";

      if (map3 is Map<String, String>) {
        map3["bbb"] = "wangwu";
      }
      for (var key in map3.keys) {
        print("$key--${map3[key]}");
      }

      //var只是一种语法，类似JavaScript，赋值的时候确定了具体的类型
      var obj1;
      obj1 = 123;
      obj1 = "123";

      //所有的类的父类都是Object，所以Object可以表示任意类型的变量
      //Object是在运行时候确定实际类型的
      Object obj2;
      obj2 = "123";
      obj2 = -123;
//      obj2 ++;// 编译直接报错

      //as 关键字 可以强制转换
      print((obj2 as int).abs());

      //dynamic是在运行时确定实际类型的
      dynamic obj3 = "123";
      //is关键字可以进行运行时检测
      if (obj3 is int) {
        obj3 ++; //运行时才报错，可以添加检测
      }

    }
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
            title: Text("基础数据类型")
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: _test,
          child: Text("click"),
        ),
      ),
    );
  }
}